const {BrowserWindow} = require('electron')
const isDev = require('electron-is-dev');
const path = require('path');

let win;
const create = async () => {
  win = new BrowserWindow({
    //https://www.electronjs.org/docs/latest/api/browser-window#new-browserwindowoptions

    width: 802,
    height: 550,

    //https://www.electronjs.org/docs/latest/api/frameless-window/#transparent-window
    //https://www.electronjs.org/docs/latest/api/frameless-window/#limitations
    transparent: true,
    //设置为false 则禁止用户调整窗口大小
    resizable: false,
    //https://www.electronjs.org/docs/latest/api/frameless-window/#draggable-region
    //设置为false 则创建的window没有工具栏和边框
    //注意: 设置为无框架窗口后, 默认是不能拖动的, 要想拖动, 需要给目标元素的style属性上添加 `-webkit-app-region: drag` 属性
    frame: false,

    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js'),
      enableRemoteModule: true
      // contextIsolation: true,
      // enableRemoteModule: false,
      // devTools: true
    }
  });

  if (isDev) {
    //the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load).
    await win.loadURL(process.env.MAIN_WINDOW_URL);
  } else {
    await win.loadFile(path.resolve(__dirname, '../../../renderer/pages/main/dist/index.html'))
  }

  //监听robot消息 并时候用robojs进行响应
  require('./robot')();
};

const send = (channel, ...args) => {
  win.webContents.send(channel, ...args);
};

const close = () => {
  win.close()
};

const minimize = () => {
  win.minimize();
};

//Restores the window from minimized state to its previous state.
const restore = () => {
  win.restore();
};

const show = () => {
  win.show();
};

module.exports = {
  create,
  send,
  close,
  minimize,
  restore,
  show,
  win
};
